<?php
namespace App\Models\TalentModels;
use App\Models\ParentModel;
use App\Models\PartnerModels\Partner;
use DB;
use App\Components\Tools;
/**
 * 用户监控model
 * @author puppet
 */
class Talent extends ParentModel{
	/**
	* 与模型关联的数据表。
	*
	* @var string
	*/
	protected $table = 'pt_talent';

	/**
	* DB::直接操作的数据库表名
	* @var string
	*/
	protected $db_table = 'pt_talent';

	/**
	* 主键 默认id。
	*
	* @var string
	*/
	protected $primaryKey = 'id';

	/**
	* 指定是否模型应该被戳记时间。
	*
	* @var bool
	*/
	public $timestamps = false;

	//查询行业列表
	function selectInd(){
		$sql="select id,name from pt_industry where status = ?";
		$values=[1];
		return DB::select($sql,$values);
	}

    //查询合伙人或者会员name
    function selectpname($pid){
        $sql="select name from pt_partner where id= ?";
        $values=[$pid];
        return DB::select($sql,$values);
    }
    //获取对应达人的视频数据
    public function getTalentData($parames){
      // print_r(111);exit();
        $openid=isset($parames['openid']) ? $parames['openid'] : '';
        $imgUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
        $partner_id=session('vip_user')[0]['admin_id'];
        $user_type = isset($params['user_type']) ? intval($params['user_type']) : 1;
        $title=isset($params['video_title']) ? $params['video_title'] : '';
        $nickname=isset($params['nickname']) ? $params['nickname'] : '';
        $res = false;
        $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $limit_sta = 0;
        $page = 1;
        if (isset($params['page']) && intval($params['page']) > 0) {
            $limit_sta = intval($limit_num * intval($params['page'] - 1));
            $page = intval($params['page']);
        }

            $count_sql="select count(distinct(a.id)) as count from pt_dy_user_video as a left join pt_talent as b on  b.open_id=a.open_id";
            $count_sql.=" where a.user_type=2 and a.open_id='$openid' ";

            $sql="select distinct a.*,b.tl_uname from pt_dy_user_video as a left join pt_talent as b on  b.open_id=a.open_id";
            $sql.=" where a.user_type=2 and a.open_id='$openid'";
            if($title){
                $count_sql.=" and (a.title like '%{$title}%' or a.talk like '%{$title}%')";
                $sql.=" and (a.title like '%{$title}%' or a.talk like '%{$title}%')";
            }
            if($nickname){
                $count_sql.=" and b.nickname like '%{$nickname}%'";
                $sql.=" and b.nickname like '%{$nickname}%'";
            }
        $size=20;
        // echo $sql;exit;
        $count = DB::select($count_sql);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $sql .= " order by a.update_time desc LIMIT $limit_sta,$limit_num";
        $data_list = DB::select($sql);
     //   print_r($data_list);exit();
        $nextPage=$page < ceil($count/$size) ? $page+1 : 0;
        $html="";
        if($data_list){

            foreach($data_list as $k=>$v){
                $html.="<tr>";
                if($v['status']==2){
                    $html.="<td><a href=\"{$v['share_url']}\" target=\"_blank\"><img src=\"{$v['cover']}\" width=\"80\" height=\"100\"></a></td>";
                }else{
                    $html.="<td><a href=\"{$imgUrl}/{$v['share_url']}\" target=\"_blank\"><img src=\"{$imgUrl}/{$v['cover']}\" width=\"80\" height=\"100\"></a></td>";
                }
                $html.="<td>{$v['title']}</td>";
                $name=DB::select("select user_name from pt_partner where  id={$v['partner_id']}");
                $name=isset($name[0]['user_name']) ? $name[0]['user_name'] : 0;
                $html.="<td>$name</td>";
                $html.="<td>{$v['digg_count']}</td>";
                $html.="<td>{$v['forward_count']}</td>";
                $html.="<td>{$v['comment_count']}</td>";
                if($v['status']==0){
                    $html.="<td><button class=\"btn btn btn-default\">未审核</button></td>";
                }elseif ($v['status']==1){
                    $html.="<td><button class=\"btn btn btn-info\">抖音审核中</button></td>";
                }elseif($v['status']==2){
                    $html.="<td><button class=\"btn btn btn-success\">审核成功</button></td>";
                }elseif($v['status']==3){
                    $html.="<td><button class=\"btn btn btn-info\">后台审核中</button></td>";
                }else{
                    $html.="<td><button class=\"btn btn btn-danger\">审核失败</button></td>";
                }
                $html.="<td>{$v['remark']}</td>";
                $html.="<td>{$v['create_time']}</td>";
                $html.="<td>{$v['operator']}</td>";
                $html .= "</tr>";
            }

        }else{
            $html="<tr><td>无数据</td></tr>";
        }

        return ["html"=>$html,'nextPage'=>$nextPage];
    }
    /**
	*	达人 ajax 拉取数据  type 0审核  1全部
	*/
	function selectData($parames){
		$uid=isset($parames['uid']) ? $parames['uid'] : '';
		$name=isset($parames['name']) ? $parames['name'] : '';
        $username=isset($parames['username']) ? $parames['username'] : '';
		$selectedInd=isset($parames['selectedInd']) ? intval($parames['selectedInd']) : 0;
		$selectedAuth=isset($parames['selectedAuth']) ? intval($parames['selectedAuth']) : 99;
		$type=isset($parames['type']) ? intval($parames['type']) : 0;
		$size = (isset($parames['limit']) && intval($parames['limit'])>0 && intval($parames['limit']) <= 100) ? $parames['limit'] : 10;
		$page = isset($parames['page']) ? intval($parames['page']) : 1;
		$offset = ($page-1)*$size;
		
		$countSql="select count(a.id) as count from $this->table as a left join pt_dy_access_token as b on b.open_id=a.open_id ";
        $countSql.=" left join pt_partner as c on a.partner_id=c.id";
        $countSql.=" where 1 = ?";
		$sql="select a.*,c.user_name,b.status as token_status from $this->table as a left join pt_dy_access_token as b on b.open_id=a.open_id ";

        $sql.=" left join pt_partner as c on a.partner_id=c.id";
        $sql.=" where 1 = ?";
		if($name){
			$countSql.=" and a.tl_uname like '%{$name}%'";
			$sql.=" and a.tl_uname like '%{$name}%'";
		}
        if($username){
            $countSql.=" and c.user_name like '%{$username}%'";
            $sql.=" and c.user_name like '%{$username}%'";
        }
		if($uid){
			$countSql.=" and a.tl_uid like '%{$uid}%'";
			$sql.=" and a.tl_uid like '%{$uid}%'";
		}
		if($selectedInd){
			$countSql.=" and a.tl_type={$selectedInd}";
			$sql.=" and a.tl_type={$selectedInd}";
		}
		if($selectedAuth==0){
			$countSql.=" and (a.tl_auth=0 or b.status=0)";
			$sql.=" and (a.tl_auth=0 or b.status=0)";
		}elseif($selectedAuth==1){
			$countSql.=" and b.status=1";
			$sql.=" and b.status=1";
		}
		if($type==1){
			$countSql.=" and (a.tl_status=0 or a.tl_status=1)";
			$sql.=" and (a.tl_status=0 or a.tl_status=1)";
		}else{
			$countSql.=" and a.tl_status=-2";
			$sql.=" and a.tl_status=-2";
		}
		$sql.=" order by a.tl_created desc limit $offset,$size";
		// echo $sql;exit;
		$count=DB::select($countSql,[1]);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$data=DB::select($sql,[1]);
	//	print_r($data);exit();
		$indList=$this->selectInd();
		if(!empty($data) && !empty($indList) ){
			$i=0;
			foreach($data as $k=>$v){
				$data[$k]['kid']=++$i;
				foreach($indList as $ka=>$va){
					if($va['id']==$v['tl_type']){
						$data[$k]['tl_type']=$va['name'];
					}
				}
				if(!empty($v['user_name'])){
                    $data[$k]['user_name']=$v['user_name'];
                }else{
                    $data[$k]['user_name']='云快视总后台';
                }
                $data[$k]['pname']=$this->selectpname($v['partner_id']);
				$data[$k]['tl_uimg']="<img src='{$data[$k]['tl_uimg']}' style='width:50px;height:50px;border-radius:50%;'>";
				if($type==1){
					$status=$v['tl_status']==0 ? 1 : 0;
					$data[$k]['status']="<div class='checkbox checkbox-slider--b-flat'><label>";
					$data[$k]['status'].="<input type='".($status==1?'default':'checkbox')."' checked='' onclick='actionList({$v['id']},{$status})'>";
					$data[$k]['status'].="<span></span></label></div>";
                    $openid=isset($v['open_id']) ? $v['open_id']:'';
					$data[$k]['operation']="<a href=\"javascript:;\" title=\"修改\" aria-label=\"Update\" data-pjax=\"0\"><span class=\"glyphicon glyphicon-pencil\" onclick=\"upNumAll({$v['id']},'{$v['tl_uname']}',{$v['tl_fans']},{$v['tl_play']},{$v['tl_type']})\"></span></a>";
					$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;";

					$data[$k]['operation'].='<a href="javascript:;" title="删除" aria-label="Delete" data-pjax="0" onclick="deleteList('.$v['id'].')"><span class="glyphicon glyphicon-trash"></span></a>';
					$tl_auth="已授权";
					if( $data[$k]['token_status']==0 || $data[$k]['tl_auth']==0 ){
						$tl_auth="<a href=\"/douyin/warrant/info?kid={$v['id']}&industry_id={$v['tl_type']}\" style=\"color:red;\" target=\"_blank\">去授权</a>";
					}
					$data[$k]['tl_auth']=$tl_auth;
				}else{

  					$data[$k]['operation']='<button class="btn btn-info btn-xs" onclick="saveTalent('."'{$v['id']}','1'".')">通过</button>';
					$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
					$data[$k]['operation'].='<button class="btn btn-defult btn-xs" onclick="saveTalent('."'{$v['id']}','-3'".')">不通过</button>';
				}
				if(!empty($v['open_id'])){
                    $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                   // $openid="{$v['open_id']}";
                    $data[$k]['operation'].="<a href='javascript:;' title='查看达人视频' aria-label='Update' data-pjax='0' onclick=\"showshoucang('{$v['open_id']}','{$v['tl_uname']}')\"><span class='glyphicon glyphicon-film'></span></a>";

                }
			}
		}
		return ['data'=>$data,'total'=>$count];
	}
	
	//审核修改
	function updateTal($kid,$data){
		$sql="update $this->table set ";
		if(is_array($data)){
			foreach($data as $k=>$v){
				$sql.="`{$k}`='{$v}',";
			}
			$sql=substr($sql,0,-1)." where id={$kid}";
		}
		// echo $sql;exit;
		return DB::update($sql);
	}
	//插入达人数据
	function insertTal($data){
		return DB::table($this->table)->insertGetId($data);
	}
	
	//查询该抖音号是否存在
	function chackUid($tl_uid){
		$sql="select id from pt_talent where tl_uid=? and (tl_status!=-1 or tl_status!=-3)";
		$re=DB::select($sql,[$tl_uid]);
		return isset($re[0]['id']) ? $re[0]['id'] : false;
	}
	
	//查询该手机号是否存在
	function chackTell($tl_tell){
		$sql="select id from pt_talent where tl_uid=? and (tl_status!=-1 or tl_status!=-3)";
		$re=DB::select($sql,[$tl_tell]);
		return isset($re[0]['id']) ? $re[0]['id'] : false;
	}
}